/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 13:44:19
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.*;
import com.joysuch.wwyt.bp.bean.BpEmergencyTrainingDrillListBean.Search;
import com.joysuch.wwyt.bp.entity.BpEmergencyDefect;
import com.joysuch.wwyt.bp.entity.BpEmergencyExercisePlan;
import com.joysuch.wwyt.bp.entity.BpEmergencyExerciseRecord;
import com.joysuch.wwyt.bp.service.BpEmergencyTrainingDrillService;
import com.joysuch.wwyt.common.entity.CommonIndexNotify;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 应急消防管理——培训演练
 */
@RestController
@RequestMapping("/wwyt/bp-emergency-training-drill")
@Api(tags = "应急消防管理—应急演练计划")
public class BpEmergencyTrainingDrillController {

    private static final Logger logger = LoggerFactory.getLogger(BpEmergencyTrainingDrillController.class);

    @Autowired
    private BpEmergencyTrainingDrillService bpEmergencyTrainingDrillService;

    /**
     * 新增培训演练
     *
     * @param entity           待新增的培训演练相关信息
     * @param //attachmentJson 待上传的附件相关JSON
     */
    @RequestMapping("add")
    public Object add(@Valid @RequestBody BpEmergencyTrainingDrillEditBean entity) {
        try {
            // if (attachmentJson != null) {
            // Gson gson = new Gson();
            // BaseAttachmentBean attachment = gson.fromJson(attachmentJson, BaseAttachmentBean.class);
            // entity.setAttachmentId(attachment==null?null:attachment.getId());
            // }

            bpEmergencyTrainingDrillService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, e.getMessage());
        }
    }

    /**
     * 修改培训演练
     *
     * @param //entity         待修改的培训演练相关信息
     * @param //attachmentJson 待上传的附件相关JSON（可不传）
     * @return
     */
    @RequestMapping("update")
    public Object update(@Valid @RequestBody BpEmergencyTrainingDrillEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            // if (attachmentJson != null) {
            // Gson gson = new Gson();
            // BaseAttachmentBean attachment = gson.fromJson(attachmentJson, BaseAttachmentBean.class);
            // entity.setAttachmentId(attachment == null ? null : attachment.getId());
            // }
            bpEmergencyTrainingDrillService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    /**
     * 获取培训演练信息（分页）
     *
     * @param //page      分页信息
     * @param //condition 筛选条件
     */
    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            Page<BpEmergencyTrainingDrillListBean> data = bpEmergencyTrainingDrillService.findByPage(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    /**
     * 删除培训演练
     *
     * @param id 待删除的培训演练 ID
     */
    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpEmergencyTrainingDrillService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }


    /**
     * 新增演练计划
     *
     * @param bean
     * @return
     */

    @PostMapping("/saveExercisePlan")
    @ApiOperation(value = "新增-更新演练计划")
    @RequiresPermissions("bp:training:addExercisePlan")
    public ResultBean saveExercisePlan(@Valid @RequestBody BpEmergencyExercisePlanBean bean) {

        BpEmergencyExercisePlan plan = null;
        try {
            plan = bpEmergencyTrainingDrillService.saveExercisePlan(bean);
            return ResultBean.success(plan);
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(),e.getMessage());
        }

    }


    /**
     * 作废演练计划
     *
     * @param id
     * @return
     */

    @GetMapping("/deleteExercisePlan")
    @ApiOperation(value = "作废演练计划")
    @RequiresPermissions("bp:training:deleteExercisePlan")
    public ResultBean deleteExercisePlan(Long id) {
        try {
            bpEmergencyTrainingDrillService.deleteExercisePlan(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    /**
     * 查看演练计划
     *
     * @param id
     * @return
     */
    @GetMapping("/getExercisePlan")
    @ApiOperation(value = "查看演练计划")
//    @RequiresPermissions("bp:training:getExercisePlan")
    public ResultBean getExercisePlan(Long id) {
        try {
            BpEmergencyExercisePlanBean bean = bpEmergencyTrainingDrillService.getExercisePlan(id);
            return ResultBean.success(bean);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 分页展示演练计划
     *
     * @param page
     * @param condition
     * @return
     */
    @GetMapping("/pageExercisePlan")
    @ApiOperation(value = "分页展示演练计划")
//    @RequiresPermissions("bp:training:pageExercisePlan")
    public ResultBean pageExercisePlan(Pageable page, BpEmergencyExercisePlanBean.Search condition) {
        try {
            Page<BpEmergencyExercisePlanBean> data = bpEmergencyTrainingDrillService.pageExercisePlan(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }

    }

    /**
     * 新增/修改演练记录
     *
     * @param bean
     * @return
     */

    @PostMapping("/addRecord")
    @ApiOperation(value = "新增/修改演练记录")
    @RequiresPermissions("bp:training:addRecord")
    public ResultBean addRecord(@Valid @RequestBody BpEmergencyExerciseRecordBean bean) {
        try {
            BpEmergencyExerciseRecord plan = bpEmergencyTrainingDrillService.addRecord(bean);
            return ResultBean.success(plan);
        } catch (Exception e) {
            logger.error("新增失败", e);
            return ResultBean.fail(101, "新增失败");
        }
    }

    /**
     * 删除演练记录
     *
     * @param id
     * @return
     */

    @GetMapping("/deleteRecord")
    @ApiOperation(value = "删除演练记录")
    @RequiresPermissions("bp:training:addRecord")
    public ResultBean addRecord(Long id) {
        return bpEmergencyTrainingDrillService.deleteRecord(id);

    }

    /**
     * 批量删除演练记录
     *
     * @param ids
     * @return
     */

    @GetMapping("/deleteRecords")
    @ApiOperation(value = "批量删除演练记录")
    @RequiresPermissions("bp:training:addRecord")
    public ResultBean addRecord(List<Long> ids) {
        return bpEmergencyTrainingDrillService.deleteRecords(ids);

    }

//    /**
//     * 修改演练记录
//     *
//     * @param bean
//     * @return
//     */
//
//    @PostMapping("/updateRecord")
//    @ApiOperation(value = "修改演练计划")
//    public ResultBean updateRecord(@Valid @RequestBody BpEmergencyExerciseRecordBean bean) {
//        try {
//            BpEmergencyExerciseRecord plan = bpEmergencyTrainingDrillService.updateRecord(bean);
//            return ResultBean.success(plan);
//        } catch (Exception e) {
//            return ResultBean.fail(101, "修改失败");
//        }
//    }


    /**
     * 查看演练记录
     *
     * @param id
     * @return
     */
    @GetMapping("/getRecord")
    @ApiOperation(value = "查看演练记录")
//    @RequiresPermissions("bp:training:getRecord")
    public ResultBean getRecord(Long id) {
        try {
            BpEmergencyExerciseRecordBean bean = bpEmergencyTrainingDrillService.getRecord(id);
            return ResultBean.success(bean);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }


    /**
     * 更改演练记录状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updateRecord")
    @ApiOperation(value = "更改演练记录状态")
    public ResultBean updateRecord(Long id, Integer state) {
        bpEmergencyTrainingDrillService.updateRecord(id, state);
        return ResultBean.success("");

    }


    /**
     * 分页展示演练记录
     *
     * @param page
     * @param condition
     * @return
     */
    @GetMapping("/pageRecord")
    @ApiOperation(value = "分页展示演练记录")
//    @RequiresPermissions("bp:training:getRecord")
    public ResultBean pageExercisePlan(Pageable page, BpEmergencyExerciseRecordBean.Search condition) {
        try {
            Page<BpEmergencyExerciseRecordBean> data = bpEmergencyTrainingDrillService.pageRecord(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }

    }

    /**
     * 整改处理列表
     *
     * @param id
     * @return
     */
    @GetMapping("/defect")
    @ApiOperation(value = "显示整改处理列表")
//    @RequiresPermissions("bp:training:defect")
    public ResultBean defect(Long id) {
        try {
            List<BpEmergencyDefectBean> list = bpEmergencyTrainingDrillService.defect(id);
            return ResultBean.success(list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 整改操作
     *
     * @param id
     * @return
     */
    @GetMapping("/defectAction")
    @ApiOperation(value = "整改通知/立即整改")
    @RequiresPermissions("bp:training:defect")
    public ResultBean defectAction(Long id) {
        try {
            BpEmergencyDefect bdf = bpEmergencyTrainingDrillService.defectAction(id);
            return ResultBean.success(bdf);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

//    /**
//     * 应急通知
//     *
//     * @param id
//     * @return
//     */
//    @GetMapping("/notifyPlan")
//    @ApiOperation(value = "应急通知")
//    @RequiresPermissions("bp:training:notifyPlan")
//    public ResultBean notifyPlan(Long id) {
//        try {
//             bpEmergencyTrainingDrillService.notifyPlan(id);
//            return ResultBean.success("");
//        } catch (Exception e) {
//            return ResultBean.fail(101, "通知发送失败");
//        }
//    }
}
